package edu.uoc.tdp.pac4.server.connexiomanteniment.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import edu.uoc.tdp.pac4.server.connexiomanteniment.exceptions.*;


public class Gestor {
	public Connection connection;
    private final String propertiesFile = new File("").getAbsolutePath() + "/properties/configuration.properties"; 
	private final String driver = "org.postgresql.Driver";

	public Gestor() {	}

	public boolean connect() throws ExceptionServer {
		try {
			// Load the JDBC driver for Postgre
			Class.forName(driver);
			// Get DB connection parameters from properties file
			Properties prop = new Properties();
			prop.load(new FileInputStream(propertiesFile));       
			String url = prop.getProperty("url");
			String user = prop.getProperty("username");
			String password= prop.getProperty("password");
			//Connect to DB
			connection = DriverManager.getConnection(url, user, password);
			return true;
		} catch (ClassNotFoundException e) { //JDBC driver not found
			throw new ExceptionServer(ExceptionServer.SERVER_DB_DRIVER_NOT_FOUND);
		} catch (FileNotFoundException e) {//Property file to connect to DB not found
			throw new ExceptionServer(ExceptionServer.SERVER_DB_PROPERTY_FILE_NOT_FOUND);
		} catch (IOException e) { //Incorrect property file
			throw new ExceptionServer(ExceptionServer.SERVER_DB_INCORRECT_PROPERTY_FILE);
		} catch (SQLException e) { //Incorrect login, DB stopped or unexistant 
			throw new ExceptionServer(ExceptionServer.SERVER_DB_CONNECTION_FAILED);
		} catch (Exception e){//Any other error
			throw new ExceptionServer(ExceptionServer.SERVER_DB_UNEXPECTED_ERROR);
		} 
	}
	
	public boolean disconnect() throws ExceptionServer {
/*		try {
		   connection.close();
		} catch (SQLException e) {
			throw new ExceptionServer("Server.DB.DisconnectionFailed");
		}
*/		return true;
	}
	
	public void ManageSQLException(SQLException e) throws ExceptionServer {
		ExceptionServer es = null;
		if (e.getMessage().startsWith(ExceptionServer.SQLERROR_VALUE_TOO_LONG)) es = new ExceptionServer(ExceptionServer.SERVER_DB_VALUE_TOO_LONG); 
		else if  (e.getMessage().startsWith(ExceptionServer.SQLERROR_NULL_VALUE)) es = new ExceptionServer(ExceptionServer.SERVER_DB_NULL_VALUE);
		else if  (e.getMessage().startsWith(ExceptionServer.SQLERROR_DUPLICATED_KEY)) {
			 es = new ExceptionServer(ExceptionServer.SERVER_DB_CONSTRAINT);
			 if (e.getMessage().contains("usuari_email_key")) es = new ExceptionServer(ExceptionServer.SERVER_DB_CONSTRAINTEMAIL); 
			 if (e.getMessage().contains("usuari_idusuari_key")) es = new ExceptionServer(ExceptionServer.SERVER_DB_CONSTRAINTLOGIN); 
		} else if  (e.getMessage().startsWith(ExceptionServer.SQLERROR_RELATION)) es = new ExceptionServer(ExceptionServer.SERVER_DB_UNKNONW_TABLE);
   		else if  (e.getMessage().startsWith(ExceptionServer.SQLERROR_COLUMN)) es = new ExceptionServer(ExceptionServer.SERVER_DB_UNKNOWN_COLUMN);
   		else if  (e.getMessage().startsWith(ExceptionServer.SQLERROR_MISSING_VALUE)) es = new ExceptionServer(ExceptionServer.SERVER_DB_MISSING_VALUE);
   		else es = new ExceptionServer(ExceptionServer.SERVER_DB_UNEXPECTED_ERROR);
		es.setAdditionalInfo(e.getMessage()); throw es;
	}
	
	public Connection getConnetion() { return connection; }
}	

